#include "codeconvertor.h"
#include <QStringList>
#include <QTextCodec>
#include <QDebug>

CodeConvertor::CodeConvertor()
{

}

QString CodeConvertor::utf8ToUnicode(const QString &in)
{
    QString outstr;
    QByteArray byteArr = in.toLatin1();


    for (int i = 0;i < byteArr.length();) {
        if ((byteArr[i] & 0x80) == 0x00)
        {//0x0xxxxxx 只占一个字节
            outstr += "00";
            if (!(byteArr[i] & 0xf0)) {
                outstr += "0";
            }
            outstr += QString::number((uint)byteArr[i] & 0x000000ff,16);
            ++i;
        }
        else
        {

            if ((byteArr[i] & 0xe0) == 0xc0)
            {//0x110xxxxx 0x10xxxxxx 占两个字节
                char t1 = byteArr[i] & 0x1f;//第一个字节的后五位 1
                char t2 = byteArr[++i] & 0x3f;//第而个字节的后六位 2
                char t3 = t2 | ((t1 & 0x03) << 6);//2
                char t4 = (t1 >> 2) & 07;//1
                if (!(t4 & 0xf0)) {
                    outstr += "0";
                }
                outstr += QString::number((uint)t4 & 0x000000ff,16);
                if (!(t3 & 0xf0)) {
                    outstr += "0";
                }
                outstr += QString::number((uint)t3 & 0x000000ff,16);
                ++i;

            }
            else
            {
                if ((byteArr[i] & 0xf0) == 0xe0)
                {//占3个字节
                    char t1 = byteArr[i] & 0x0f;//1
                    char t2 = byteArr[++i] & 0x3f;//2
                    char t3 = byteArr[++i] & 0x3f;//3
                    char t4 = t3 | ((t2 & 0x03) << 6);//3
                    char t5 = ((t2 >> 2) & 0x0f) | ((t1 << 4) & 0xf0) ;//2
                    if (!(t5 & 0xf0)) {
                        outstr += "0";
                    }
                    outstr += QString::number((uint)t5 & 0x000000ff,16);
                    if (!(t4 & 0xf0)) {
                        outstr += "0";
                    }
                    outstr += QString::number((uint)t4 & 0x000000ff,16);

                    ++i;

                }

            }
        }
    }

    return outstr;

}
QByteArray CodeConvertor::_utf8ToUnicode(const std::string &in)
{
    QByteArray outstr;
    char buf[3];
    for (int i = 0;i < (int)in.length();) {
        if ((in[i] & 0x80) == 0x00)
        {//0x0xxxxxx 只占一个字节
            //  CodeConvertor::charTo2Hex(0x00,buf);
            outstr += "00";//buf;
            CodeConvertor::charTo2Hex(in[i],buf);
            outstr += buf;
            ++i;
        }
        else
        {
            if ((in[i] & 0xe0) == 0xc0)
            {//0x110xxxxx 0x10xxxxxx 占两个字节
                char t1 = in[i] & 0x1f;//第一个字节的后五位 1
                char t2 = in[++i] & 0x3f;//第而个字节的后六位 2
                char t3 = t2 | ((t1 & 0x03) << 6);//2
                char t4 = (t1 >> 2) & 07;//1
                CodeConvertor::charTo2Hex(t4,buf);
                outstr += buf;
                CodeConvertor::charTo2Hex(t3,buf);
                outstr += buf;
                ++i;

            }
            else
            {
                if ((in[i] & 0xf0) == 0xe0)
                {//占3个字节
                    char t1 = in[i] & 0x0f;//1
                    char t2 = in[++i] & 0x3f;//2
                    char t3 = in[++i] & 0x3f;//3
                    char t4 = t3 | ((t2 & 0x03) << 6);//3
                    char t5 = ((t2 >> 2) & 0x0f) | ((t1 << 4) & 0xf0) ;//2

                    CodeConvertor::charTo2Hex(t5,buf);
                    outstr += buf;

                    CodeConvertor::charTo2Hex(t4,buf);
                    outstr += buf;

                    ++i;

                }

            }
        }
    }
    return outstr;
}
void CodeConvertor::charTo2Hex(char in, char *out)
{
    char l = in & 0x0f;
    char h = (in & 0xf0) >> 4;
    int hh = h % 16;
    if (hh < 10) {
        out[0] = '0' + hh;
    } else {
        out[0] = hh - 10 + 'A';
    }
    int ll = l % 16;
    if (ll < 10) {
        out[1] = '0' + ll;
    } else {
        out[1] = ll - 10 + 'A';
    }

    out[2] = '\0';
}
QByteArray CodeConvertor::changeAddr(const QString &addr)
{
    QByteArray out;

    out.append(addr);
    /**
     *
     *去掉号码前面的‘+’
     */

    if (out[0] == '+') {
        out.remove(0,1);
    }

    if (out.length() & 1) {
        out.append("F");
    }

    for (int i = 0;i < out.length() - 1;i+=2) {
        char t = out[i];
        out[i] = out[i+1];
        out[i+1] = t;
    }

    out.push_front("91");



    int len = out.length() / 2;

    /*
     *
     *一般不会超过 0x0f
     */
    char t[3] = {'\0'};
    if (len < 10) {
        t[0] = '0';
        t[1] = '0' + len;
    } else {
        t[0] = '0';
        t[1] = len - 10 + 'A';
    }
    //out = t + out;
    out.prepend(t);

    return out;
}
QByteArray CodeConvertor::changePhone(const QString &addr)
{
    QByteArray out;

    out.append(addr);
    /**
     *
     *去掉号码前面的‘+’
     */

    if (out[0] == '+') {
        out.remove(0,1);
    }

    if (out.length() & 1) {
        out.append("F");
    }

    for (int i = 0;i < out.length() - 1;i+=2) {
        char t = out[i];
        out[i] = out[i+1];
        out[i+1] = t;
    }

    return out;

}

QString CodeConvertor::unicodeToUtf8(const QString &in)
{
    QStringList s;
    QTextCodec *codec = QTextCodec::codecForName("UTF-8");

    for(int i = 0;i < in.length();i += 4) {
        s.append(in.mid(i,4));
    }


    QString t1;

    foreach (const QString &t, s) {
        t1.append(t.toUShort(0,16));
    }


    return codec->fromUnicode(t1);
}

QString CodeConvertor::date_time_frame(const QString &c)
{

    QByteArray out;
    out.append(c);
    for (int i = 0;i < out.length() - 1;i+=2) {
        char t = out[i];
        out[i] = out[i+1];
        out[i+1] = t;
    }

    for (int i = 1;i < 3;++i) {

        out.insert(2 * i + (i-1),'-');
    }

    out.insert(8,' ');

    for (int i = 1;i < 3;++i) {

        out.insert(9 + 2 * i + (i-1),':');
    }
    return out;


}

QString CodeConvertor::phone_frame(const QString &c)
{
    QByteArray out;
    out.append(c);
    for (int i = 0;i < out.length() - 1;i+=2) {
        char t = out[i];
        out[i] = out[i+1];
        out[i+1] = t;
    }

    out.remove(out.length()-1,1);

    return out;


}


